﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DomainDriver.Presentation.Silverlight.Base;
using AutomotivePricingModel.Companies;

namespace AutomotivePricingSilverlightApp.Controls.Companies
{
    public partial class CompanyEditorView : UserControl, IDomainView
    {
        private CompanyEditorController m_Controller;
        private bool m_UpdatingDisplay = false;

        public CompanyEditorView()
        {
            InitializeComponent();
            m_Controller = new CompanyEditorController();
            m_Controller.View = this;
            Loaded += new RoutedEventHandler(CompanyEditorView_Loaded);
            TypedController.ApplicationObject.StateContainer.StateChanged += new StateChangeHandler(StateContainer_StateChanged);
            TypedController.RefreshRequested += new RefreshRequestHandler(Controller_RefreshRequested);
        }

        private void CompanyEditorView_Loaded(object sender, RoutedEventArgs e)
        { DomainViewExtensions.ValidateAndRefresh(this); }

        private void StateContainer_StateChanged(object sender, StateChangeEventArgs args)
        {
            if (sender == TypedController)
            { return; }

            if (args.StateName != CompanyEditorController.CompanySelectionName)
            { return; }

            DomainViewExtensions.ValidateAndRefresh(this);
        }

        private void Controller_RefreshRequested(object sender, RefreshRequestEventArgs args)
        { DomainViewExtensions.ValidateAndRefresh(this); }

        public IDomainController Controller
        {
            get { return m_Controller; }
        }

        public CompanyEditorController TypedController
        {
            get { return m_Controller; }
        }

        public void LockView()
        {
            // do nothing
        }

        public void UnlockView()
        {
            // do nothing
        }

        public void ClearView()
        {
            m_UpdatingDisplay = true;
            m_CompanyNameTextbox.Text = string.Empty;
            m_CeoNameTextbox.Text = string.Empty;
            m_UpdatingDisplay = false;
        }

        public void ReloadView()
        {
            m_UpdatingDisplay = true;

            Company company = TypedController.GetSelectedCompany();
            BindToCompany(company);

            m_UpdatingDisplay = false;
        }

        private void BindToCompany(Company company)
        {
            m_CompanyNameTextbox.Text = company.CompanyName;
            m_CeoNameTextbox.Text = company.CeoName;
        }

        private void m_CompanyNameTextbox_KeyUp(object sender, KeyEventArgs e)
        {
            if (m_UpdatingDisplay)
            { return; }

            if (e.Key == Key.Escape)
            {
                ReloadView();
                return;
            }
            else if (e.Key == Key.Enter)
            {
                string companyName = m_CompanyNameTextbox.Text;
                TypedController.UpdateCompanyName(companyName);
            }
        }

        private void m_CeoNameTextbox_KeyUp(object sender, KeyEventArgs e)
        {
            if (m_UpdatingDisplay)
            { return; }

            if (e.Key == Key.Escape)
            {
                ReloadView();
                return;
            }
            else if (e.Key == Key.Enter)
            {
                string ceoName = m_CeoNameTextbox.Text;
                TypedController.UpdateCeoName(ceoName);
            }
        }
    }
}